<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout"> 退出 </el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff"
                 unique-opened :collapse="isCollapse" :collapse-transition="false"
                 :router="true" :default-active="activePath">  <!-- :default-active="$route.path" -->
          <!-- 一级菜单 -->
          <el-submenu v-for="menu in menuList" :key="menu.id" :index="menu.id+''">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <i :class="iconsObj[menu.id]"></i> <!-- 图标 -->
              <span>{{menu.authName}}</span> <!-- 文本 -->
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="subMenu in menu.children" :key="subMenu.id"
                          :index="'/'+subMenu.path" @click="saveNavState('/'+subMenu.path)">
              <template slot="title">
                <i class="el-icon-menu"></i> <!-- 图标 -->
                <span>{{subMenu.authName}}</span> <!-- 文本 -->
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体结构 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src
export default {
  name: 'Home',
  data() {
    return {
      menuList: [], // 左侧菜单数据
      iconsObj: { // 字体对象
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false, // 左侧菜单是否折叠
      activePath: '' // 被激活的链接地址
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出功能实现原理：基于token的方式实现退出比较简单,只要销毁本地的token即可。
    // 这样后续的请求就不会携带token,必须重新登陆生成一个新的token之后才可以访问页面
    logout() {
      window.sessionStorage.clear()
      this.$message.info('退出登录')
      this.$router.replace('/login')
    },

    // 获取所有的菜单
    async getMenuList() {
      const { data: result } = await this.$http.get('menus') // result是data属性的别名
      // console.log(result)
      if (result.meta.status !== 200) {
        this.$message.error(result.meta.msg)
      }
      this.menuList = result.data
    },

    // 点击按钮, 切换左侧菜单的打开与折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    // 保存链接的激活状态 -- 解决页面刷新带来的问题
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container {
    width: 100%;
    height: 100%;
  }
  .el-header {
    background-color: #373D41;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
      img {
        width: 40px;
        height: 40px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      color: #fff;
      line-height: 24px;
      text-align: center;
      letter-spacing: 0.2em;  /*给文本之间设置距离*/
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 10px;
  }
</style>
